package com.tools.developer;

import java.lang.ref.WeakReference;
import java.util.Calendar;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.Window;
import android.view.WindowManager;

import com.tools.developer.app.AppListActivity;
import com.tools.developer.util.SharedPrefHelper;

public class SplashActivity extends Activity {

	private boolean flag;

	private SharedPrefHelper mCfg;
	private static final int MSG_GO_MAIN = 0x1001;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		final Window win = getWindow();
		win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		win.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN); // 隐藏状态栏
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.activity_app_start);

		mCfg = SharedPrefHelper.getInstanse(getApplicationContext());
		mHandler.sendEmptyMessageDelayed(MSG_GO_MAIN, 1500);
	}

	MyHandler mHandler = new MyHandler(this);

	static class MyHandler extends Handler {
		WeakReference<Activity> mActivity;

		MyHandler(Activity activity) {
			mActivity = new WeakReference<Activity>(activity);
		}

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_GO_MAIN:
				mActivity.get().startActivity(
						new Intent(mActivity.get(),
								AppListActivity.class));

				break;
			}

		}
	}

	@Override
	public void onStart() {
		super.onStart();

		mCfg.getFirstRunDate();

		// 设置屏幕参数
		setScreenPara(mCfg);

		// 初始化"退出"标志位
		mCfg.putExitFlag(false);
		long reportTime = mCfg.getReportTime();
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(reportTime);
		int lastday = calendar.get(Calendar.DAY_OF_YEAR);

		calendar = Calendar.getInstance();
		int today = calendar.get(Calendar.DAY_OF_YEAR);
		flag = true;
		if (lastday == today) {
			flag = false;
			return;
		}

	}

	@Override
	public void onStop() {
		super.onStop();
		if (flag) {
			Calendar calendar = Calendar.getInstance();
			SharedPrefHelper.getInstanse(getApplicationContext())
					.setReportTime(calendar.getTimeInMillis());
		}
	}

	private void setScreenPara(SharedPrefHelper configManager) {
		DisplayMetrics metric = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metric);
		configManager.putScreenDensity(metric.density);
		configManager.putScreenWidth(metric.widthPixels);// 当前屏幕像素
		configManager.putScreenHeigth(metric.heightPixels);
	}
}